package org.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.example.entity.Role;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**
 * 角色数据访问接口
 */
@Mapper
public interface RoleMapper extends BaseMapper<Role> {
    
    /**
     * 统计每个平台角色下的用户数量（从 user_role_platform_relations 统计）
     */
    @Select("SELECT role_id AS roleId, COUNT(*) AS userCount FROM user_role_platform_relations WHERE is_active = 1 GROUP BY role_id")
    List<Map<String, Object>> countPlatformUsersByRoleId();
    
    /**
     * 将指定角色的用户转移到默认角色
     */
    @Update("UPDATE users SET primary_role_id = #{defaultRoleId} WHERE primary_role_id = #{oldRoleId}")
    int transferUsersToDefaultRole(@Param("oldRoleId") Long oldRoleId, @Param("defaultRoleId") Long defaultRoleId);
}










